<?php

namespace Admin\Controller;


class CategoryController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        $this->categories = $this->all_categories();
    }

    /**
     * 查出商品的所有分类
     */
    public function all_categories()
    {
        $categories = $this->category->where("parent_id = 0")->order('sort_order desc')->select();
        foreach ($categories as &$category) {
            $category['children'] = $this->category->where("parent_id = $category[id]")->select();
        }
        return $categories;
    }

    /**
     *商品分类首页
     */
    public function index()
    {
        $this->display();
    }

    /**
     * 新增商品分类
     */
    public function add()
    {

        if (IS_POST) {
            $data['parent_id'] = I('post.parent_id');
            $data['name'] = trim(I("post.name"));
            $data['sort_order'] = trim(I("post.sort_order"));
            $data['is_show'] = I('post.is_show');
            $data['description'] = I('post.description');

            if (strlen($data['name']) < 2 || strlen($data['name']) > 20) {
                $this->error('分类名不能小于2位或者大于20位');
            }
            if (strlen($data['description']) == "") {
                $this->error('描述内容不能为空');
            }

            $this->category->create();
            $this->category->add($data);
            $this->success('新增分类成功', U('Category/index'));
        } else {
            $categries = $this->category->where("parent_id = 0")->select();
            $this->assign('categories', $categries);
            $this->display();
        }
    }

    /**
     * 编辑商品分类
     */
    public function edit()
    {

        $id = I('get.id');
        if (IS_POST) {
            $data['parent_id'] = I('post.parent_id');
            $data['name'] = trim(I("post.name"));
            $data['sort_order'] = trim(I("post.sort_order"));
            $data['is_show'] = I('post.is_show');
            $data['description'] = I('post.description');

            if (strlen($data['name']) < 2 || strlen($data['name']) > 20) {
                $this->error('分类名不能小于2位或者大于20位');
            }
            if (strlen($data['description']) == "") {
                $this->error('描述内容不能为空');
            }

            $this->category->create();
            $this->category->where("id = '$id'")->save($data);

            $this->success('编辑分类成功', U('Category/index'));
        } else {
            $cate = $this->category->where("id = '$id'")->find();
            $this->assign('cate', $cate);
            $this->display();
        }
    }

    /**
     * ajax删除
     */
    public function ajax_delete()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $count = $this->category->where("parent_id = '$id'")->count();
            if ($count) {
                $info = array('status' => 0, 'msg' => '此分类下有二级分类，不能删除');
            } else {
                $this->category->where("id = '$id'")->delete();
                $info = array('status' => 1, 'msg' => '删除成功');
            }
            $this->ajaxReturn($info);
        }
    }

    /**
     * ajax多选删除
     */
    public function ajax_delete_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $id) {
                $this->category->where("id = '$id'")->delete();
            }
            $info = array('status' => 1, 'msg' => '删除多个分类成功');
            $this->ajaxReturn($info);
        }
    }

    /**
     * ajax排序
     */
    public function ajax_sort_order()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $sort_order = I('post.sort_order');
            $this->category->where("id = $id")->setField('sort_order', $sort_order);
        }
    }

    /**
     * 改变商城分类显示状态
     */
    public function ajax_is_show()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $is_show = !I('post.is_show');

            $this->category->where("id = '$id'")->setField('is_show', $is_show);
        }
    }

}
